package com.easylive.web.controller;

import cn.hutool.core.bean.BeanUtil;
import com.easylive.annotation.GlobalInterceptor;
import com.easylive.entity.constants.Constants;
import com.easylive.entity.dto.TokenUserInfoDto;
import com.easylive.entity.enums.PageSize;
import com.easylive.entity.enums.UserActionTypeEnum;
import com.easylive.entity.enums.VideoOrderTypeEnum;
import com.easylive.entity.po.UserAction;
import com.easylive.entity.po.UserFocus;
import com.easylive.entity.po.UserInfo;
import com.easylive.entity.po.VideoInfo;
import com.easylive.entity.query.UserActionQuery;
import com.easylive.entity.query.UserFocusQuery;
import com.easylive.entity.query.VideoInfoQuery;
import com.easylive.entity.vo.PaginationResultVO;
import com.easylive.entity.vo.ResponseVO;
import com.easylive.entity.vo.UserInfoVO;
import com.easylive.service.UserActionService;
import com.easylive.service.UserFocusService;
import com.easylive.service.UserInfoService;
import com.easylive.service.VideoInfoService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.constraints.*;

@RestController
@RequestMapping("/uhome")
@Validated
public class UHomeController extends ABaseController{
    @Resource
    private UserInfoService userInfoService;

    @Resource
    private VideoInfoService videoInfoService;

    @Resource
    private UserActionService userActionService;

    @Resource
    private UserFocusService userFocusService;

    @RequestMapping("/getUserInfo")
    public ResponseVO getUserInfo(@NotEmpty String userId){
        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto();
        UserInfo userInfo = userInfoService.getUserDetailInfo(tokenUserInfoDto == null ? null : tokenUserInfoDto.getUserId() , userId);

        UserInfoVO userInfoVo = BeanUtil.copyProperties(userInfo, UserInfoVO.class);
        userInfoVo.setAvatar(userInfo.getAvater());


        return getSuccessResponseVO(userInfoVo);
    }

    @RequestMapping("/updateUserInfo")
    @GlobalInterceptor(checkLogin = true)
    public ResponseVO updateUserInfo(@NotEmpty @Size(max = 20) String nickName,
                                     @NotEmpty @Size(max = 100) String avatar,
                                     @NotNull Integer sex,
                                     @NotEmpty @Size(max = 100)String personIntroduction,
                                     @NotEmpty @Size(max = 100)String birthday,
                                     @NotEmpty @Size(max = 150)String school,
                                     @Size(max = 300) String noticeInfo){

        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto();
        UserInfo userInfo = new UserInfo();

        userInfo.setUserId(tokenUserInfoDto.getUserId());
        userInfo.setNickName(nickName);
        userInfo.setAvater(avatar);
        userInfo.setSex(sex);
        userInfo.setPersonIntroduction(personIntroduction);
        userInfo.setBirthday(birthday);
        userInfo.setSchool(school);
        userInfo.setNoticeInfo(noticeInfo);

        userInfoService.updateUserInfo(userInfo,tokenUserInfoDto);
        return getSuccessResponseVO(null);
    }

    @RequestMapping("/saveTheme")
    @GlobalInterceptor(checkLogin = true)
    public ResponseVO saveTheme(@NotNull Integer theme){

        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto();
        UserInfo userInfo = new UserInfo();
        userInfo.setTheme(theme);
        userInfoService.updateUserInfoByUserId(userInfo, tokenUserInfoDto.getUserId());
        return getSuccessResponseVO(null);
    }

    @RequestMapping("/focus")
    @GlobalInterceptor(checkLogin = true)
    public ResponseVO focus(@NotEmpty String focusUserId){
        userFocusService.focusUser(getTokenUserInfoDto().getUserId(),focusUserId);
        return getSuccessResponseVO(null);
    }

    @RequestMapping("/cancelFocus")
    @GlobalInterceptor(checkLogin = true)
    public ResponseVO cancelFocus(@NotEmpty String focusUserId){
        userFocusService.cancelFocusUser(getTokenUserInfoDto().getUserId(),focusUserId);
        return getSuccessResponseVO(null);
    }

    @RequestMapping("/loadFocusList")
    @GlobalInterceptor(checkLogin = true)
    public ResponseVO loadFocusList(Integer pageNo){

        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto();
        UserFocusQuery query =  new UserFocusQuery();
        query.setUserId(tokenUserInfoDto.getUserId());
        query.setPageNo(pageNo);
        query.setOrderBy("focus_time desc");
        query.setQueryType(Constants.ZERO);
        PaginationResultVO<UserFocus> paginationResultVO = userFocusService.findListByPage(query);
        return getSuccessResponseVO(paginationResultVO);

    }

    @RequestMapping("/loadFansList")
    @GlobalInterceptor(checkLogin = true)
    public ResponseVO loadFansList(Integer pageNo){

        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto();
        UserFocusQuery query =  new UserFocusQuery();
        query.setFocusUserId(tokenUserInfoDto.getUserId());
        query.setPageNo(pageNo);
        query.setOrderBy("focus_time desc");
        query.setQueryType(Constants.ONE);
        PaginationResultVO<UserFocus> paginationResultVO = userFocusService.findListByPage(query);
        return getSuccessResponseVO(paginationResultVO);

    }

    @RequestMapping("/loadVideoList")
    @GlobalInterceptor(checkLogin = true)
    public ResponseVO loadVideoList(@NotEmpty String userId,
                                    Integer type,
                                    Integer pageNo,
                                    String videoName,
                                    Integer orderType){
        VideoInfoQuery  infoQuery = new VideoInfoQuery();
        if(type != null){
            infoQuery.setPageSize(PageSize.SIZE10.getSize());
        }
        VideoOrderTypeEnum videoOrderTypeEnum = VideoOrderTypeEnum.getByType(orderType);
        if(videoOrderTypeEnum == null){
            videoOrderTypeEnum = VideoOrderTypeEnum.CREATE_TIME;
        }
        infoQuery.setOrderBy(videoOrderTypeEnum.getField() + " desc");
        infoQuery.setVideoNameFuzzy(videoName);
        infoQuery.setUserId(userId);
        infoQuery.setPageNo(pageNo);
        PaginationResultVO<VideoInfo> paginationResultVO = videoInfoService.findListByPage(infoQuery);
        return getSuccessResponseVO(paginationResultVO);



    }

    @RequestMapping("/loadUserCollection")
    @GlobalInterceptor(checkLogin = true)
    public ResponseVO loadUserCollection(@NotEmpty String userId,Integer pageNo){
        UserActionQuery query = new UserActionQuery();
        query.setActionType(UserActionTypeEnum.VIDEO_COLLECT.getType());
        query.setUserId(userId);
        query.setPageNo(pageNo);
        query.setOrderBy("action_time desc");
        query.setQueryVideoInfo(true);
        PaginationResultVO<UserAction> paginationResultVO = userActionService.findListByPage(query);
        return getSuccessResponseVO(paginationResultVO);



    }

}
